<template>
  <div class='diaPadding'>
    <el-scrollbar :style="`width:100%; height: ${height || '100%'};`" >
      <div style="min-width: 480px">
        <el-tabs @tab-click='handleClick'>
          <el-tab-pane label='本地上传'>
            <div class='diaCon localUpdate'>
              <div class='filelist'>
                <MinUpload ref='minUpload'></MinUpload>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label='班课资料' v-if='isTeach()'>
            <div class='diaCon' v-if='materArr.length'>
              <div class='filelist2' v-loading='loading'>
                <div class='row top'>
                  <div class='item'>选择</div>
                  <div class='item'>文件名</div>
                  <div class='item'>大小</div>
                  <div class='item'>上传时间</div>
                </div>

                <div class='row' v-for='(item,index) in materArr' :key='item.id'>
                  <div class='item'>
                    <el-checkbox v-model='item.checked' size='large' @change='selfFileChange(index)'/>
                  </div>
                  <div class='item'>{{ giveEllipsisStr(item.name, 40) }}</div>
                  <div class='item'>{{ filterSize(item.byteCount) }}</div>
                  <div class='item'>{{ parseTime(item.createTime) }}</div>
                </div>
              </div>
            </div>

            <div class='diaCon' v-else>
              <el-empty description='暂无资料'/>
            </div>

          </el-tab-pane>
          <el-tab-pane label='公共资料库' v-if='isTeach()'>
            <div class='diaCon'>
              <el-empty description='开发中，待开放...'/>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-scrollbar>
  </div>
</template>

<script setup>
import {materialList} from '@/api/banke/class';
import {filterSize, giveEllipsisStr, parseTime} from '@/utils/common';
import {ElMessage} from 'element-plus';
import {isTeach} from '@/utils/auth';
import MinUpload from '@/components/MinUpload.vue';


const materArr = ref([]);
//已选中的数组
const selfSelectArr = ref([]);
const loading = ref(true);


const cancelState = ref(false);

defineProps({
  bigCfmState: {
    type: Boolean,
    default: true,
  },
  height: {
    type: String
  }
});

const emits = defineEmits(['update:bigCfmState']);


//点击标签页按钮
const handleClick = (e) => {
  if (e.index == 1) {
    getMaterial();
  }
};


//获取个人班课资料库
const getMaterial = (keyname = '') => {
  materialList({id: sessionStorage.getItem('bkid'), name: keyname}).then((res) => {

      if (res.code == 200) {
        materArr.value = res.rows;
        loading.value = false;
      } else {
        ElMessage.error('获取错误，请刷新');
      }
    },
  );
};

//点击自己课程资料库复选框
const selfFileChange = (index) => {
  let row = materArr.value[index];
  console.log(row);
  if (materArr.value[index].checked) {
    let obj = {
      ossId: row.ossId,
      customName: row.name,
      size: row.byteCount,
      originalName: row.originalName,
      uploadFileName: row.uploadFileName,
    };
    selfSelectArr.value.push(obj);
  } else {
    selfSelectArr.value.forEach((i, idx) => {
      if (i.ossId == row.ossId) selfSelectArr.value.splice(idx, 1);
    });
  }
};

//监听自己资料的数组变化
watch(selfSelectArr.value, (nv, ov) => {
  if (nv.length > 0 || ov.length > 0) {
    emits('update:bigCfmState', false);
  }
});


defineExpose({
  selfSelectArr,
  cancelState,
  materArr,
});


</script>

<style lang='scss' scoped>
.diaPadding {
  padding-bottom: 30px;

  .diaCon {
    .filelist {
      .btnadding {
        width: 100px;
        line-height: 30px;
        text-align: center;
        border: 1px solid #4395ff;
        color: #4395ff;
        font-size: 14px;
        margin: 0 auto;
        margin-top: 30px;
      }

      .row {
        display: flex;
        align-items: center;
        height: 50px;
        font-size: 15px;

        .item:nth-child(1) {
          width: 50%;
        }

        .item:nth-child(2) {
          width: 10%;
        }

        .item:nth-child(3) {
          width: 230px;
          display: flex;

          .progress {
            height: 6px;
            width: 170px;
            background: #eee;
            border-radius: 10px;
            position: relative;
            overflow: hidden;

            .line {
              width: 0%;
              height: 6px;
              background: #4395ff;
              position: absolute;
              top: 0;
              left: 0;
              transition: 0.3s;
            }
          }

          .num {
            width: 50px;
            font-size: 12px;
            padding-left: 5px;
          }
        }

        .item:nth-child(4) {
          width: calc(40% - 220px);
        }
      }


    }

    .filelist2 {
      .row {
        display: flex;
        align-items: center;
        height: 43px;
        font-size: 14px;
        border-bottom: 1px solid #efefef;

        &:hover {
          background: #fafafa;
        }

        .item:nth-child(1) {
          width: 7%;
        }

        .item:nth-child(2) {
          width: 53%;
        }

        .item:nth-child(3) {
          width: 15%;
        }

        .item:nth-child(4) {
          width: 25%;
        }
      }
    }


    .top {
      background: #f5f7fa;
      font-size: 13px;
      height: 40px;
    }

    .item {
      padding: 0 10px;
      display: flex;
      align-items: center;

      .iconfont {
        font-size: 20px;
        cursor: pointer;
      }

      .coloricon {
        font-size: 23px;
        margin-right: 5px;
      }
    }
  }
}
</style>
